<?php
    class Page {
        private static $_instance;
        private $_document;
        private $_template_set;
        
        private function __construct() {
            $this->_template_set = false;
            $this->_document = new DOMDocument();
        }
        
        public static function getInstance() {
            if (!isset(self::$_instance)) {
                self::$_instance = new Page();
            }
            
            return self::$_instance;
        }
        
        ///Renders the page given by pgName in the content section of the template
        ///PRE-CONDITION: The DOMDocument has been loaded with a template.
        ///POST-CONDITION: content div contains the page content
        public function render($pgName) {
            if (isset($this->_document)) {
                
                $db = Database::getInstance();
                $rslts = $db->select(array('page_name', 'page_css', 'page_script'), 
                                     array('page_name'=>$pgName));
                                     
                for ($i = 0; $i < $rslts->numItems(); $i++) {
                    $this->addStyleSheet($rslts->findItem('page_css'));
                    if ($rslts->findItem('page_script') != '') {
                        $this->addJavascript($rslts->findItem('page_script'));
                    }
                }
                
                $content = $this->_document->createDocumentFragment();
                $file = INCLUDES_DIR . FILE_SEP . TEMPLATES_DIR  . FILE_SEP . $rslts->findItem('page_name') . ".tpl";
                $src = file_get_contents($file);
                $content->appendXML($src);
                $this->_document->getElementById('content')->appendChild($content);
            }
            
            echo $this->_document->saveXML();
        }
        
        public function update($widget) {
            
            $this->render();
        }
        
        public function set_template($template) {
            if (!$this->_template_set) { //load the right template.
                $this->_document->loadHTMLFile(INCLUDES_DIR . FILE_SEP . TEMPLATES_DIR  . FILE_SEP . $template . ".tpl");
                $this->_template_set = true;
            }
        }
        
        private function addStyleSheet($stylesheet) {
            $head = $this->_document->getElementsByTagName('head')->item(0);
            
            $link = $this->_document->createElement('link');
            $typetxt = $this->_document->createTextNode('text/css');
            $reltxt = $this->_document->createTextNode('stylesheet');
            $hreftxt = $this->_document->createTextNode("/styles/$stylesheet.css");
            
            $type = $this->_document->createAttribute('type');
            $rel = $this->_document->createAttribute('rel');
            $href = $this->_document->createAttribute('href');
            
            $type->appendChild($typetxt);
            $rel->appendChild($reltxt);
            $href->appendChild($hreftxt);
            
            $link->appendChild($type);
            $link->appendChild($rel);
            $link->appendChild($href);
            
            $head->appendChild($link);
        }
        
        private function addJavascript($script) {
            $head = $this->_document->getElementsByTagName('head')->item(0);
            
            $link = $this->_document->createElement('script');
            $typetxt = $this->_document->createTextNode('text/javascript');
            $hreftxt = $this->_document->createTextNode("/scripts/$script.js");
            
            $type = $this->_document->createAttribute('type');
            $href = $this->_document->createAttribute('src');
            
            $type->appendChild($typetxt);
            $href->appendChild($hreftxt);
            
            $link->appendChild($type);
            $link->appendChild($href);
            
            $head->appendChild($link, $this->_document->createTextNode(' '));
        }
    }
?>